package com.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jt.mapper.ItemCatMapper;
import com.jt.pojo.Item;
import com.jt.pojo.ItemCat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ItemCatServiceImpl implements ItemCatService {
    @Autowired
    private ItemCatMapper itemCatMapper;


    @Override
    public List<ItemCat> findItemCatList(Integer level) {
        long start = System.currentTimeMillis();
        QueryWrapper<ItemCat> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", 0);
        List<ItemCat> oneList = itemCatMapper.selectList(queryWrapper);
        for (ItemCat oneItemCat : oneList) {
            int oneId = oneItemCat.getId();
            queryWrapper.clear();
            queryWrapper.eq("parent_id", oneId);
            List<ItemCat> twoList = itemCatMapper.selectList(queryWrapper);
            for (ItemCat towItemCat : twoList) {
                int towId = towItemCat.getId();
                queryWrapper.clear();
                queryWrapper.eq("parent_id", towId);
                List<ItemCat> threeList = itemCatMapper.selectList(queryWrapper);
                towItemCat.setChildren(threeList);
            }
            oneItemCat.setChildren(twoList);
        }
        long end = System.currentTimeMillis();
        System.out.println(end - start);
        return oneList;
    }
}
